//
//  ClockStatusBarHUD.swift
//  SkyAclock
//
//  Created by lx on 2020/11/18.
//

import UIKit

class SkyStatusBarHUD: UIView {

    static let manager = SkyStatusBarHUD(frame: CGRect(x: WIDTH * 0.2, y: -48 * 2, width: WIDTH * 0.6, height: 48))
    
    let BarHeight: CGFloat = 36

    class func showDesc(_ desc: String) {
        let manager = SkyStatusBarHUD.manager
        AppTool.rootView()?.addSubview(manager)
        
        manager.showDesc(desc: desc)
    }
    
    override init (frame: CGRect ) {
        super.init (frame: frame)
        
        self.layer.cornerRadius = self.height * 0.5
        self.layer.shadowOffset = .zero
        self.layer.shadowRadius = interval
        self.layer.shadowOpacity = 1
        self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: self.layer.cornerRadius).cgPath
    }
    
    required init ?(coder aDecoder: NSCoder ) {
        fatalError( "init(coder:) has not been implemented" )
    }
    
    func showDesc(desc: String) {
        self.show()
        
        self.backgroundColor = .bg
        self.layer.shadowColor = UIColor.shadow.cgColor
        self.descLabel.textColor = .detail
        
        self.descLabel.text = desc
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
            self.disappear()
        }
    }
    
    func show() {
        UIView.animate(withDuration: 0.36) {
            self.top = STATUEBAR_HEIGHT + interval * 0.5
        } completion: { (complete) in
        }

    }
    
    func disappear() {
        UIView.animate(withDuration: 0.36) {
            self.top = -self.BarHeight * 2
        } completion: { (complete) in
            self.removeFromSuperview()
        }
    }
    
    lazy var descLabel: UILabel = {
        let descLabel = UILabel(string: "", fontSize: 13, color: .detail, textAlignment: .center)
        descLabel.adjustsFontSizeToFitWidth = true
        self.addSubview(descLabel)
        descLabel.snp.makeConstraints { (make) in
            make.left.equalTo(interval)
            make.right.equalTo(-interval)
            make.top.equalTo(interval * 0.5)
            make.bottom.equalTo(-interval * 0.5)
        }
        return descLabel
    }()

}
